
#include <RawAccelData.h>


/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 Modified 16 Jun 2009
 By Tom Igoe
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */
 
RawAccelData oAccData;

const int xAccPin = 0;
const int yAccPin = 1;
const int zAccPin = 2;
const int xGyroPin = 3;
const int yGyroPin = 4;
const int zGyroPin = 5;

int xAccel = 0;
 int yAccel = 0;
 int zAccel = 0;
 
 int xRotation = 0;
 int yRotation = 0;
 int zRotation = 0;
 
 int xOld = 0;
 int xOld1 = 0;
 int xOld2 = 0;
 int xOld3 = 0;
 
 int nSamplesInGesture = 0;
 
unsigned int xTimeAverage = 0;
unsigned int yTimeAverage = 0;
unsigned int zTimeAverage = 0;
unsigned int nSamples = 0;
boolean bStartFlag = false;
boolean bEndFlag = false;
boolean bGestureOccuring = false;

char sSerialString[512];

void setup() {
  // declare the ledPin as an OUTPUT:
 // pinMode(ledPin, OUTPUT);  
 
 //for comm with computer use one of 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
 Serial.begin(9600);
 
 analogReference(EXTERNAL);
 pinMode(xAccPin, INPUT);
 pinMode(yAccPin, INPUT);
 pinMode(zAccPin, INPUT);
 pinMode(xGyroPin, INPUT);
 pinMode(yGyroPin, INPUT);
 pinMode(zGyroPin, INPUT);
 oAccData.init();
 }

void loop() {
  /*delay(10);
  nSamples++;
  bStartFlag = false;
  bEndFlag = false;
  if(nSamplesInGesture >= 200){
    bEndFlag = true;
    bGestureOccuring = false;
  }
  
  nSamples = (nSamples + 1);

  
  // read the value from the sensor:
  xOld3 = xOld2;
  xOld2 = xOld1;
  xOld1 = xOld;
  xOld = xAccel;*/
  
  int xAccelOld = xAccel;
  int yAccelOld = yAccel;
  int zAccelOld = zAccel;
  int xGyroOld = xRotation;
  int yGyroOld = yRotation;
  int zGyroOld = zRotation;
  
  
  xAccel = analogRead(xAccPin);
  yAccel = analogRead(yAccPin);
  zAccel = analogRead(zAccPin);
  
  xRotation = analogRead(xGyroPin);
  yRotation = analogRead(yGyroPin);
  zRotation = analogRead(zGyroPin);
  

 /* if(abs(xAccel - xAccelOld) > 15){
 Serial.print(xAccel);
 Serial.print(" ");
 Serial.println(xAccelOld);
  }
 delay(50);*/
 
 //Uncomment next 3 lines to use with Processing
 sprintf(sSerialString, "%d,%d,%d,%d,%d,%d", xAccel, yAccel, zAccel, xRotation, yRotation, zRotation);
 Serial.print(sSerialString);
 Serial.print(10, BYTE);

 /*   sSerialString[0] = 50;
    sSerialString[1] = 48;
    sSerialString[2] = 44;
    sSerialString[3] = 52;
    sSerialString[4] = 55;*/

  

 /* int fU = 0;
  while(sSerialString[fU] != '\0'){
        Serial.print((int) sSerialString[fU]);
        Serial.print(",  ");
        Serial.println(fU);
    fU++;

  }
  Serial.println("-----");*/
  //Serial.println(sizeof(char));
  //delay(750);
 // Serial.println(fU);
 // sprintf(sSerialString, "%d,%d,%d,%d,%d,%d,%d", 1, 2, 3, 4, 5, 6, 7 );
//  Serial.print(sSerialString);
//Serial.print(10, BYTE); // lf in ascii

 /* if(abs(xAccel - xOld3) > 10 && nSamples > 10) {
   // Serial.print(xAccel); Serial.print("  "); Serial.print(xOld3); Serial.print("  "); Serial.println(abs(xAccel - xOld3));
    bStartFlag = true;
  }
  else if ((abs(xAccel - xOld3) <= 10) && bGestureOccuring){
    bEndFlag = true;
  }
  yAccel = analogRead(yAccPin);
  zAccel = analogRead(zAccPin);  

  

 // Serial.println(xAccel);
 Serial.print("      ");
  Serial.print(yAccel);
  Serial.print("      ");
  Serial.println(zAccel);
  
  if(bStartFlag && ! bGestureOccuring){
   Serial.println("Start flag raised.");
   bGestureOccuring = true;
   Serial.println(nSamplesInGesture);
  }
  if(bGestureOccuring){
    oAccData.addToXBuffer(xAccel);
    oAccData.addToYBuffer(yAccel);
    oAccData.addToZBuffer(zAccel);
    nSamplesInGesture++;
  }
  if(bEndFlag){
    Serial.println("End flag raised.");
       Serial.println(nSamplesInGesture);
    bGestureOccuring = false;
    nSamplesInGesture = 0;
    forever();
    
  }
  */
//  delay(100);
 // delay(10);
                 
}



void forever(){
  for(int i = 0; i < 200; i++){
    if(oAccData.xBuf(i) != -1){
      Serial.print("x[");Serial.print(i);Serial.print("] = ");Serial.print(oAccData.xBuf(i));
      Serial.print(" y[");Serial.print(i);Serial.print("] = ");Serial.print(oAccData.yBuf(i));
      Serial.print(" z[");Serial.print(i);Serial.print("] = ");Serial.println(oAccData.zBuf(i));
    }
  }
  while(true){}
}
















